<?php
//发送验证码

include('../WeSql/widget.php');
include('../WeSql/config.php');
session_start();//开启session
@$mail = $_REQUEST["mail"];
$mail = "3088007572@qq.com";

if ($mail == '') {
    $array = [
        'code' => '-1',
        'msg' => '参数不完整'
    ];
    exit(json_encode($array, JSON_UNESCAPED_UNICODE));
}

if(!$IS_REG_CODE){
    $array = [
        'code' => '-1',
        'msg' => '未开启注册验证码'
    ];
    exit(json_encode($array, JSON_UNESCAPED_UNICODE));
}

if (isset($_SESSION['view'])) {
    $view = $_SESSION['view'];
} else {
    $view = 0;
}
if (time() - $view < 60) {
    $array = [
        'code' => '-1',
        'msg' => '60秒内只可发送一次验证码'
    ];
    exit(json_encode($array, JSON_UNESCAPED_UNICODE));
} else {
    $_SESSION['view'] = time();
    $code = null;
    for ($i = 0; $i < 4; $i++) {
        $code = $code . mt_rand(0, 9);
    }
    $mcode = base64_encode($code);
    $mcode = base64_encode($mcode);
    $mcode = base64_encode($mcode);
    setcookie('mcode', $mcode, time() + $COOKEI_STORAGE_TIME, '/');

    $mailContent = file_get_contents("../WeSql/phpmailer/mail.html");
    $mailContent = str_replace('${code}',$code,$mailContent);
    $mailContent = str_replace('${deep_time}',$COOKEI_STORAGE_TIME / 60,$mailContent);

    if (sendMail($mail, "后台验证码", $mailContent)) {
        $array = [
            'code' => '1',
            'msg' => '验证码发送成功'
        ];
        echo json_encode($array, JSON_UNESCAPED_UNICODE);
    } else {
        $array = [
            'code' => '1',
            'msg' => '验证码发送失败'
        ];
        echo json_encode($array, JSON_UNESCAPED_UNICODE);
    }

}
